import 'package:flutter/material.dart';
import 'package:event_bus/event_bus.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:part_time_job_pro/common/api/http_req.dart';
import 'package:part_time_job_pro/common/api/rest_client.dart';
import 'package:part_time_job_pro/common/langs/local.dart';
import 'package:part_time_job_pro/common/langs/translation_service.dart';
import 'package:part_time_job_pro/common/routers/pages.dart';
import 'package:part_time_job_pro/common/store/config.dart';
import 'package:part_time_job_pro/common/style/theme.dart';
import 'package:part_time_job_pro/common/utils/logger.dart';
import 'package:part_time_job_pro/common/values/server_config.dart';
import 'package:part_time_job_pro/global.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'common/cache/app_box.dart';

//全局参数
Size defaultSize = const Size(360, 690);
Size androidScreenSize = const Size(360, 690);
Size windowsScreenSize = const Size(1080, 1920);
Size webScreenSize = const Size(360, 690);
//全局 key
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
//全局接口请求
late RestClient restClient;
//跨组件消息通信
EventBus eventBus = EventBus();

Future<void> main() async {
  //实例化请求接口
  restClient = RestClient(HttpReq().dio!, baseUrl: HttpConfig.serverUrl);
  //全局初始化
  await Global.init();
  //启动应用
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      ///与键盘是否遮挡输入框有关
      rebuildFactor: RebuildFactors.sizeAndViewInsets,
      minTextAdapt: true,
      splitScreenMode: true,
      designSize: defaultSize,
      builder: (_, child) => RefreshConfiguration(
        headerBuilder: () => const ClassicHeader(),
        footerBuilder: () => const ClassicFooter(),
        hideFooterWhenNotFull: true,
        headerTriggerDistance: 80,
        maxOverScrollExtent: 100,
        footerTriggerDistance: 150,
        child: GetMaterialApp(
          key: navigatorKey,
          title: '怪兽兼职',
          theme: AppTheme.lightTheme,
          darkTheme: AppTheme.darkTheme,
          //themeMode: ThemeMode.system,
          themeMode: AppBox.shared.theme == 0
              ? MediaQuery.of(context).platformBrightness == Brightness.dark
                    ? ThemeMode.dark
                    : ThemeMode.light
              : AppBox.shared.theme == 1
              ? ThemeMode.light
              : ThemeMode.dark,
          debugShowCheckedModeBanner: false,
          initialRoute: AppPages.initRoute,
          getPages: AppPages.routes,
          //builder: EasyLoading.init(),
          //FlutterEasyLoading 插件需要初始化
          builder: (context, widget) {
            return MediaQuery(
              ///设置文字大小不随系统设置改变
              data: MediaQuery.of(context).copyWith(textScaler: const TextScaler.linear(1.0)),
              child: FlutterEasyLoading(child: widget),
            );
          },
          translations: TranslationService(),
          navigatorObservers: [AppPages.observer],
          //路由观察
          localizationsDelegates: const [
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            GlobalCupertinoLocalizations.delegate,
          ],
          supportedLocales: ConfigStore.to.languages,
          // locale: ConfigStore.to.locale,
          locale: Local.getLocale(),
          fallbackLocale: TranslationService.fallbackLocale,
          enableLog: true,
          logWriterCallback: Logger.write,
        ),
      ),
    );
  }
}
